using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class P_Move : MonoBehaviour//相机
{
    public GameObject target;
    Vector3 offset;
    // Start is called before the first frame update
    void Start()
    {
        transform.LookAt(target.transform.position);
        transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
        offset = target.transform.position - transform.position;
    }

    // Update is called once per frame
    void LateUpdate()
    {
        //Rotate();//мת
        Rollup();
        Follow();
    }
    public float zoomSpeed = 1f;
    float zoom;
    void Follow()
    {
        zoom = Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
        if (zoom != 0)
        {
            offset -= zoom * offset;
        }
        transform.position = target.transform.position - offset;
    }
    public float rotationSpeed = 500f;
    private bool isRotating, lookup = false;
    float mousex, mousey;
    void Rotate()
    {
        if (Input.GetMouseButtonDown(1))
        {
            isRotating = true;
        }
        if (Input.GetMouseButtonUp(1))
        {
            isRotating = false;
        }
        if (isRotating)
        {
            mousex = Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
            transform.RotateAround(target.transform.position, Vector3.up, mousex);
            offset = target.transform.position - transform.position;
        }
    }
    void Rollup()
    {
        if (Input.GetMouseButtonDown(2))
        {
            lookup = true;
        }
        if (Input.GetMouseButtonUp(2))
        {
            lookup = false;
        }
        if (lookup)
        {
            mousey = Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
            transform.RotateAround(target.transform.position, transform.right, mousey);
            offset = target.transform.position - transform.position;
        }
    }
}
